package com.liu.student.mapper;

import com.liu.student.entity.Course;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CourseMapper {
    @Select("SELECT * FROM course")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "courseCode", column = "course_code"),
        @Result(property = "name", column = "name"),
        @Result(property = "description", column = "description"),
        @Result(property = "credits", column = "credits"),
        @Result(property = "capacity", column = "capacity"),
        @Result(property = "selectedCount", column = "selected_count"),
        @Result(property = "teacherId", column = "teacher_id"),
        @Result(property = "status", column = "status"),
        @Result(property = "createdAt", column = "created_at"),
        @Result(property = "updatedAt", column = "updated_at")
    })
    List<Course> findAll();

    @Select("SELECT * FROM course WHERE status = 'ACTIVE'")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "courseCode", column = "course_code"),
        @Result(property = "name", column = "name"),
        @Result(property = "description", column = "description"),
        @Result(property = "credits", column = "credits"),
        @Result(property = "capacity", column = "capacity"),
        @Result(property = "selectedCount", column = "selected_count"),
        @Result(property = "teacherId", column = "teacher_id"),
        @Result(property = "status", column = "status"),
        @Result(property = "createdAt", column = "created_at"),
        @Result(property = "updatedAt", column = "updated_at")
    })
    List<Course> findAvailableCourses();

    @Select("SELECT * FROM course WHERE teacher_id = #{teacherId}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "courseCode", column = "course_code"),
        @Result(property = "name", column = "name"),
        @Result(property = "description", column = "description"),
        @Result(property = "credits", column = "credits"),
        @Result(property = "capacity", column = "capacity"),
        @Result(property = "selectedCount", column = "selected_count"),
        @Result(property = "teacherId", column = "teacher_id"),
        @Result(property = "status", column = "status"),
        @Result(property = "createdAt", column = "created_at"),
        @Result(property = "updatedAt", column = "updated_at")
    })
    List<Course> findByTeacherId(Long teacherId);

    @Select("SELECT * FROM course WHERE id = #{id}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "courseCode", column = "course_code"),
        @Result(property = "name", column = "name"),
        @Result(property = "description", column = "description"),
        @Result(property = "credits", column = "credits"),
        @Result(property = "capacity", column = "capacity"),
        @Result(property = "selectedCount", column = "selected_count"),
        @Result(property = "teacherId", column = "teacher_id"),
        @Result(property = "status", column = "status"),
        @Result(property = "createdAt", column = "created_at"),
        @Result(property = "updatedAt", column = "updated_at")
    })
    Course findById(Long id);

    @Insert("INSERT INTO course (course_code, name, description, credits, capacity, selected_count, teacher_id, status) " +
            "VALUES (#{courseCode}, #{name}, #{description}, #{credits}, #{capacity}, #{selectedCount}, #{teacherId}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Course course);

    @Update("UPDATE course SET course_code = #{courseCode}, name = #{name}, description = #{description}, " +
            "credits = #{credits}, capacity = #{capacity}, selected_count = #{selectedCount}, " +
            "teacher_id = #{teacherId}, status = #{status} WHERE id = #{id}")
    int update(Course course);

    @Delete("DELETE FROM course WHERE id = #{id}")
    int deleteById(Long id);

    @Select("SELECT COUNT(*) FROM course")
    long count();

    // 乐观名额扣减：只有在未满的情况下才+1
    @Update("UPDATE course SET selected_count = selected_count + 1 " +
            "WHERE id = #{courseId} AND selected_count < capacity")
    int tryIncreaseSelected(Long courseId);

    @Update("UPDATE course SET selected_count = selected_count - 1 " +
            "WHERE id = #{courseId} AND selected_count > 0")
    int tryDecreaseSelected(Long courseId);
}